package club.gskj.gulimall.member.controller;

import java.util.Arrays;
import java.util.EmptyStackException;
import java.util.Map;

import club.gskj.common.exception.BizCodeEnume;
import club.gskj.gulimall.member.exception.PhoneExistException;
import club.gskj.gulimall.member.exception.UserNameExistException;
import club.gskj.gulimall.member.feign.CouponFeignService;
import club.gskj.gulimall.member.vo.MemberLoginVo;
import club.gskj.gulimall.member.vo.MemberRegisterVo;
import club.gskj.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import club.gskj.gulimall.member.entity.MemberEntity;
import club.gskj.gulimall.member.service.MemberService;
import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.R;


/**
 * 会员
 *
 * @author 康纳-connor
 * @email 2417039231@gmail.com
 * @date 2020-12-27 15:51:57
 */
@RestController
@RequestMapping("member/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 远程接口服务
     */
    @Autowired
    private CouponFeignService couponFeignService;

    /**
     * 远程调用接口
     */
    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("康纳");

        // 远程调用,优惠券服务
        R r = couponFeignService.memberCoupon();
        Object coupons = r.get("coupons");
        return R.ok().put("member", memberEntity).put("coupons", coupons);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * @param memberRegisterVo
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.3.15 21:48
     * @Description 注册用户信息
     */
    @PostMapping("/register")
    public R registerMember(@RequestBody MemberRegisterVo memberRegisterVo) {
        try {
            // service 发送异常捕获后，返回响应结果
            memberService.register(memberRegisterVo);
        } catch (UserNameExistException u) {
            return R.error(BizCodeEnume.USERNAME_EXIST_CODE.getCode(), BizCodeEnume.USERNAME_EXIST_CODE.getMsg());
        } catch (PhoneExistException p) {
            return R.error(BizCodeEnume.PHONE_EXIST_CODE.getCode(), BizCodeEnume.PHONE_EXIST_CODE.getMsg());
        } catch (Exception e) {
            return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /***
     *  账号、密码用户登陆
     * @param vo
     * @return
     */
    @PostMapping("/login")
    public R memberLogin(@RequestBody MemberLoginVo vo) {

        MemberEntity memberEntity = memberService.login(vo);
        if (null == memberEntity) {
            // 登陆失败，返回错误信息
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_CODE.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_CODE.getMsg());
        }
        return R.ok().setData(memberEntity);
    }

    /***
     *  社交用户登陆
     * @param vo
     * @return
     */
    @PostMapping("/auth2/login")
    public R authLogin(@RequestBody SocialUser vo) throws Exception {

        MemberEntity memberEntity = memberService.login(vo);
        if (null == memberEntity) {
            // 登陆失败，返回错误信息
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_CODE.getCode(), BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_CODE.getMsg());
        }
        return R.ok().setData(memberEntity);
    }

}
